home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / speaker_lib.pas < prev    next >
Pascal/Delphi Source File  |  2001-04-11  |  5KB  |  195 lines

  1. {////////////////////////////////////////////////////////////////
  2. // File - speaker_lib.pas
  3. //
  4. // This application plays a tone to the speaker, and is
  5. // controlled via a graphical user interface - speaker_gui
  6. // The speaker is accessed directly on the motherboard, using
  7. // WinDriver functions.
  8. //
  9. // this file contain the access functions to the speaker
  10. // using WinDriver.
  11. ////////////////////////////////////////////////////////////////}
  12.  
  13.  
  14. unit Speaker_Lib;
  15.  
  16. interface
  17.  
  18. uses
  19.   Windows, windrvr, bits;
  20.  
  21. const
  22.  SPEAKER_IO_42 = 0;
  23.  SPEAKER_IO_43 = 1;
  24.  SPEAKER_IO_61 = 2;
  25.  SPEAKER_ITEMS = 3;
  26.  SPEAKER_IO_ADDR42 = $42 ;
  27.  SPEAKER_IO_ADDR43 = $43 ;
  28.  SPEAKER_IO_ADDR61 = $61 ;
  29.  
  30. type
  31.   SPEAKER_STRUCT = record
  32.     hWD: HANDLE;
  33.     cardReg: WD_CARD_REGISTER;
  34.   end;
  35.   SPEAKER_HANDLE = ^SPEAKER_STRUCT;
  36.  
  37.   function SPEAKER_Open(var hSPEAKER: SPEAKER_HANDLE ): boolean;
  38.   procedure SPEAKER_Close(hSPEAKER: SPEAKER_HANDLE);
  39.   procedure SPEAKER_Tone(hSPEAKER: SPEAKER_HANDLE; dwHz: DWORD ; dwMilli: DWORD);
  40.  
  41. var
  42.   SPEAKER_ErrorString: string[255];
  43.  
  44. implementation
  45.  
  46. { internal function for the SPEAKER_Open function }
  47. procedure SPEAKER_SetCardElements(hSPEAKER: SPEAKER_HANDLE);  
  48. type 
  49.   WD_ITEMS_A = array [0..SPEAKER_ITEMS] of WD_ITEMS;
  50. var
  51.   pItem: ^WD_ITEMS_A;
  52. begin
  53.     hSPEAKER^.cardReg.Card.dwItems:= SPEAKER_ITEMS;
  54.     pItem := @hSPEAKER.cardReg.Card.Item;
  55.     { SPEAKER IO range }
  56.     pItem^[SPEAKER_IO_42].item:= ITEM_IO;
  57.     pItem^[SPEAKER_IO_42].fNotSharable:= Integer(FALSE);
  58.     pItem^[SPEAKER_IO_42].IO.dwAddr:= SPEAKER_IO_ADDR42;
  59.     pItem^[SPEAKER_IO_42].IO.dwBytes:= 1;
  60.     pItem^[SPEAKER_IO_43].item:= ITEM_IO;
  61.     pItem^[SPEAKER_IO_43].fNotSharable:= Integer(FALSE);
  62.     pItem^[SPEAKER_IO_43].IO.dwAddr:= SPEAKER_IO_ADDR43;
  63.     pItem^[SPEAKER_IO_43].IO.dwBytes:= 1;
  64.     pItem^[SPEAKER_IO_61].item:= ITEM_IO;
  65.     pItem^[SPEAKER_IO_61].fNotSharable:= Integer(FALSE);
  66.     pItem^[SPEAKER_IO_61].IO.dwAddr:= SPEAKER_IO_ADDR61;
  67.     pItem^[SPEAKER_IO_61].IO.dwBytes:= 1;
  68. end;
  69.  
  70. function SPEAKER_Open (var hSPEAKER: SPEAKER_HANDLE ): boolean;
  71.   label exit,finish;
  72. var
  73.   ver: SWD_VERSION;
  74. begin
  75.   GetMem(hSPEAKER,sizeof(SPEAKER_STRUCT));
  76.   hSPEAKER^.cardReg.hCard:= 0;
  77.   FillChar(hSPEAKER^, sizeof(hSPEAKER^), 0);
  78.  
  79.   hSPEAKER^.hWD := INVALID_HANDLE_VALUE;
  80.   hSPEAKER^.hWD := WD_Open();
  81.   if hSPEAKER^.hWD=INVALID_HANDLE_VALUE then
  82.   begin
  83.     SPEAKER_ErrorString:= 'Cannot open WinDriver device';
  84.     goto exit;
  85.   end;
  86.   { cheak if using the current version }
  87.   FillChar(ver, sizeof(ver), 0);
  88.   WD_Version(hSPEAKER^.hWD,ver);
  89.   if ver.dwVer<WD_VER then
  90.   begin
  91.     SPEAKER_ErrorString:= 'error - incorrect WinDriver version';
  92.     goto exit;
  93.   end;
  94.  
  95.   SPEAKER_SetCardElements(hSPEAKER);
  96.   hSPEAKER^.cardReg.fCheckLockOnly:= DWORD (FALSE);
  97.   WD_CardRegister(hSpeaker^.hWD, hSpeaker^.cardReg);
  98.   if (hSPEAKER^.cardReg.hCard=0) then
  99.   begin
  100.     SPEAKER_ErrorString:= 'error - could not lock device';
  101.     goto exit;
  102.   end;
  103.  
  104.   {open finished OK}
  105.   SPEAKER_Open:= TRUE;
  106.   goto finish;
  107.  
  108. exit:
  109.   {error during open}
  110.   if (hSPEAKER^.cardReg.hCARD<>0) then
  111.     WD_CardUnregister(hSPEAKER^.hWD, hSPEAKER^.cardReg);
  112.   if (hSPEAKER^.hWD<>INVALID_HANDLE_VALUE) then
  113.     WD_Close(hSPEAKER^.hWD);
  114.   FreeMem(hSPEAKER,sizeof(SPEAKER_STRUCT));
  115.   SPEAKER_Open:= FALSE;
  116.  
  117. finish:
  118.  
  119. end;
  120.  
  121. procedure SPEAKER_Close (hSPEAKER: SPEAKER_HANDLE);
  122. begin
  123.   { unregister card }
  124.   if (hSPEAKER^.cardReg.hCard<>0) then
  125.     WD_CardUnregister(hSPEAKER^.hWD, hSPEAKER^.cardReg);
  126.  
  127.   { close WinDriver }
  128.   WD_Close(hSPEAKER^.hWD);
  129.  
  130.   FreeMem(hSPEAKER,sizeof(SPEAKER_STRUCT));
  131. end;
  132.  
  133. procedure SPEAKER_WriteCtrl (hSPEAKER: SPEAKER_HANDLE; data: BYTE );
  134. var
  135.   trans: SWD_TRANSFER;
  136. begin
  137.   FillChar(trans, sizeof(trans), 0);
  138.   trans.cmdTrans:= WP_BYTE;
  139.   trans.dwPort:= SPEAKER_IO_ADDR61;
  140.   trans.AByte:= data;
  141.   WD_Transfer(hSPEAKER^.hWD, trans);
  142. end;
  143.  
  144. function SPEAKER_ReadCtrl (hSPEAKER: SPEAKER_HANDLE): BYTE;
  145. var
  146.   trans: SWD_TRANSFER;
  147. begin
  148.   FillChar(trans, sizeof(trans), 0);
  149.   trans.cmdTrans:= RP_BYTE;
  150.   trans.dwPort:= SPEAKER_IO_ADDR61;
  151.   WD_Transfer(hSPEAKER^.hWD, trans);
  152.   SPEAKER_ReadCtrl:= trans.AByte;
  153. end;
  154.  
  155. procedure SPEAKER_WriteTimerData (hSPEAKER: SPEAKER_HANDLE; data: BYTE);
  156. var
  157.   trans: SWD_TRANSFER;
  158. begin
  159.   FillChar(trans, sizeof(trans), 0);
  160.   trans.cmdTrans:= WP_BYTE;
  161.   trans.dwPort:= SPEAKER_IO_ADDR42;
  162.   trans.AByte:= data;
  163.   WD_Transfer(hSPEAKER^.hWD, trans);
  164. end;
  165.  
  166. procedure SPEAKER_WriteTimerCtrl (hSPEAKER: SPEAKER_HANDLE; data: BYTE);
  167. var
  168.   trans: SWD_TRANSFER;
  169. begin
  170.   FillChar(trans, sizeof(trans), 0);
  171.   trans.cmdTrans:= WP_BYTE;
  172.   trans.dwPort:= SPEAKER_IO_ADDR43;
  173.   trans.AByte:= data;
  174.   WD_Transfer(hSPEAKER^.hWD, trans);
  175. end;
  176.  
  177. procedure SPEAKER_Tone (hSPEAKER: SPEAKER_HANDLE; dwHz: DWORD; dwMilli: DWORD);
  178. var
  179.   dwDevisor: DWORD;
  180.   bCtrl: BYTE ;
  181. begin
  182.   dwDevisor:= 1190000 div dwHz;
  183.  
  184.   SPEAKER_WriteTimerCtrl(hSPEAKER, $b6);
  185.   SPEAKER_WriteTimerData(hSPEAKER, BYTE (dwDevisor and $ff));
  186.   SPEAKER_WriteTimerData(hSPEAKER, BYTE((dwDevisor shr 8) and $ff));
  187.  
  188.   bCtrl:= SPEAKER_ReadCtrl(hSPEAKER);
  189.   SPEAKER_WriteCtrl(hSPEAKER, BYTE (bCtrl or (BIT0 or BIT1)));
  190.   Sleep(dwMilli);
  191.   SPEAKER_WriteCtrl(hSPEAKER, BYTE (bCtrl and not(BIT0 or BIT1)));
  192. end;
  193.  
  194. end.
  195.